package com.roshan.domain;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;

@Component
@Configurable
public class RsvpDataOnDemand {

    private Random rnd = new SecureRandom();

    private List<Rsvp> data;

    public Rsvp getNewTransientRsvp(int index) {
        Rsvp obj = new Rsvp();
        setAttending(obj, index);
        setCode(obj, index);
        setConfirmed(obj, index);
        setEmail(obj, index);
        setSpecialRequests(obj, index);
        return obj;
    }

    public void setAttending(Rsvp obj, int index) {
        Integer attending = new Integer(index);
        obj.setAttending(attending);
    }

    public void setCode(Rsvp obj, int index) {
        String code = "code_" + index;
        if (code.length() > 30) {
            code = code.substring(0, 30);
        }
        obj.setCode(code);
    }

    public void setConfirmed(Rsvp obj, int index) {
        Date confirmed = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(
                Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(
                Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(
                Calendar.SECOND)
                + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setConfirmed(confirmed);
    }

    public void setEmail(Rsvp obj, int index) {
        String email = "email_" + index;
        if (email.length() > 30) {
            email = email.substring(0, 30);
        }
        obj.setEmail(email);
    }

    public void setSpecialRequests(Rsvp obj, int index) {
        String specialRequests = "specialRequests_" + index;
        if (specialRequests.length() > 100) {
            specialRequests = specialRequests.substring(0, 100);
        }
        obj.setSpecialRequests(specialRequests);
    }

    public Rsvp getSpecificRsvp(int index) {
        init();
        if (index < 0)
            index = 0;
        if (index > (data.size() - 1))
            index = data.size() - 1;
        Rsvp obj = data.get(index);
        return Rsvp.findRsvp(obj.getId());
    }

    public Rsvp getRandomRsvp() {
        init();
        Rsvp obj = data.get(rnd.nextInt(data.size()));
        return Rsvp.findRsvp(obj.getId());
    }

    public boolean modifyRsvp(Rsvp obj) {
        return false;
    }

    public void init() {
        data = Rsvp.findRsvpEntries(0, 10);
        if (data == null)
            throw new IllegalStateException("Find entries implementation for 'Rsvp' illegally returned null");
        if (!data.isEmpty()) {
            return;
        }

        data = new ArrayList<com.roshan.domain.Rsvp>();
        for (int i = 0; i < 10; i++) {
            Rsvp obj = getNewTransientRsvp(i);
            try {
                obj.persist();
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> it = e.getConstraintViolations().iterator(); it.hasNext();) {
                    ConstraintViolation<?> cv = it.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage())
                            .append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            obj.flush();
            data.add(obj);
        }
    }
}
